package com.wsoft.form.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.form.entity.WfFromColumnLinkEntity;
import com.wsoft.form.mapper.WfFromColumnLinkMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/8/29 09:20
 */
@Slf4j
@Service
public class WfFormColumnLinkManager extends ServiceImpl<WfFromColumnLinkMapper, WfFromColumnLinkEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 根据 columnId 查询
     *
     * @param columnId 字段id
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Form.LINK_INFO,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFromColumnLinkEntity getByColumnId(Long columnId) {
        QueryWrapper<WfFromColumnLinkEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFromColumnLinkEntity::getColumnId, columnId);
        return getOne(query);
    }

    /**
     * 根据 columnId查询
     *
     * @param columnIdList 字段id集合
     * @return
     */
    public List<WfFromColumnLinkEntity> getByColumnId(List<Long> columnIdList) {
        List<WfFromColumnLinkEntity> list = new ArrayList<>();
        for (Long columnId : columnIdList) {
            WfFromColumnLinkEntity linkEntity = this.getByColumnId(columnId);
            if (null != linkEntity) {
                list.add(linkEntity);
            }
        }
        return list;
    }

    /**
     * 更新
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(WfFromColumnLinkEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Form.LINK_INFO + entity.getColumnId());
            redisService.del(RedisKeyConstant.Form.COLUMN_INFO + entity.getColumnId());
            redisService.del(RedisKeyConstant.Form.COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(WfFromColumnLinkEntity entity) {
        boolean b = super.save(entity);
        if(b){
            redisService.del(RedisKeyConstant.Form.COLUMN_INFO + entity.getColumnId());
            redisService.del(RedisKeyConstant.Form.COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }


    /**
     * 删除关联表
     *
     * @param columnId
     * @param linkFormId
     */
    public void remove(List<Long> columnIdList) {
        QueryWrapper<WfFromColumnLinkEntity> query = new QueryWrapper<>();
        query.lambda().in(WfFromColumnLinkEntity::getColumnId, columnIdList);
        boolean b = remove(query);
        if(b){
            redisService.del(RedisKeyConstant.Form.COLUMN_INFO + RedisKeyConstant.PATTERN_SYMBOL);
            redisService.del(RedisKeyConstant.Form.COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.LINK_PREFIX + RedisKeyConstant.PATTERN_SYMBOL);
        }
    }
}
